#include <my/Time/TriggeringTimer.h>

namespace my {

    TriggeringTimer::TriggeringTimer() : Timer(), EventHandling(), _alreadyExpired(false), _loopRunning(false)
    {

    }

    TriggeringTimer::TriggeringTimer(float timeLimit, bool initRunning, bool loopRunning) :
    Timer(timeLimit, initRunning),
    EventHandling(),
    _alreadyExpired(false),
    _loopRunning(loopRunning)
    {

    }

    TriggeringTimer::~TriggeringTimer()
    {

    }



    void    TriggeringTimer::setLoopRunning(bool active)
    {
        _loopRunning = active;
    }


    bool    TriggeringTimer::loopRunning()  const
    {
        return _loopRunning;
    }



    void    TriggeringTimer::reset(float timeLimit, bool keepRunning)
    {
        Timer::reset(timeLimit, keepRunning);

        _alreadyExpired = false;
    }

    void    TriggeringTimer::reset(bool keepRunning)
    {
        Timer::reset(keepRunning);

        _alreadyExpired = false;
    }

    void    TriggeringTimer::bind(my::slot function)
    {
        connect("isExpired", function);
    }

    void    TriggeringTimer::update()
    {
        if(isExpired() && !_alreadyExpired)
        {
            // Expiration
            _alreadyExpired = true;
            emit("isExpired");

            // If loop running, then we have to reset the timer and to re-run it
            if(_loopRunning)
                reset(true);
        }
    }



}
